﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace zielonka.co.uk.extensions.system.xml
{
    //Description:	Deserialize an XDocument to a generic type
    /*
        string hi = "hello world";
        string xml = hi.SerializeToXml();
        Console.WriteLine(xml);

        string hi2 = XDocument.Parse(xml).Deserialize<string>();
        Console.WriteLine(hi2);

        Output:

        <string>hello world</string>
        hello world
     */
    public static partial class XMLDocumentExtensions
	{
        public static T Deserialize<T>(this XDocument xmlDocument)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            using (XmlReader reader = xmlDocument.CreateReader())
                return (T)xmlSerializer.Deserialize(reader);
        }
	}
}
